using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class PoolManager : Singleton<PoolManager>
{
    public List<Cell> cellPoll = new List<Cell>();
    public GameObject cellPrefab;
    public Transform cellParent;
    public List<Text> cellTextPoll = new List<Text>();
    public GameObject cellTextPrefab;
    public Transform cellTextParent;

    private void Start()
    {
        InitPool();
    }


    public void InitPool()
    {
        int length = LevelManager.Instance.levelList[LevelManager.Instance.levelList.Count - 1].row * 
                     LevelManager.Instance.levelList[LevelManager.Instance.levelList.Count - 1].col;
        for (int i = 0; i < length; i++)
        {
            var cell =  Instantiate(cellPrefab, cellParent).GetComponent<Cell>();
            var cellText = Instantiate(cellTextPrefab, cellTextParent).GetComponent<Text>();
            cellPoll.Add(cell);
            cellTextPoll.Add(cellText);
        }
        
        Debug.LogFormat("Pool Init.  CellPoll : {0} , CellTextPoll : {1}", cellPoll.Count, cellTextPoll.Count);
    }

    public List<Cell> GetCell(int length)
    {
        Debug.LogFormat("GetCellPoll Nums: {0} ,  CellPoll Nums : {1}", length, cellPoll.Count);
        return cellPoll.GetRange(0,length);
    }

    public List<Text> GetCellText(int length)
    {
        Debug.LogFormat("GetCellTextPoll Nums: {0} ,  CellTextPoll Nums : {1}", length, cellTextPoll.Count);
        return cellTextPoll.GetRange(0,length);
    }
    
    

    public void Recycle()
    {
        foreach (Cell cell in cellPoll)
        {
            cell.transform.SetParent(cellParent);
            cell.transform.position = Vector3.zero;
        }

        foreach (Text text in cellTextPoll)
        {
            text.transform.SetParent(cellTextParent);
            text.transform.position = Vector3.zero;
        }
    }
}
